<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_bengkel_post extends CI_Model {

	function __construct(){
		parent::__construct();
	}

    // USER & Registration
	function get_users(){ // Return Class
        return $this->db->get('frum_users')->result();
    }
    
    function get_user_detail($id){ // Return Class
        return $this->db->select("*")
                ->from('frum_users a')
                ->join('frum_groups b', 'b.group_id = a.group_id')
                ->join('frum_meta c', 'c.user_id = a.user_id')
                ->join('data_kendaraan d', 'd.user_id = a.user_id')
                ->where()
                ->row();
    }
    
    function store_user(){ // Return Boolean
        $data_user['group_id']        = $this->input->post('group_id');
        $data_user['ip_address']      = $_SERVER['HTTP_HOST'];
        $data_user['username']        = $this->input->post('username');
        $data_user['password']        = $this->phpass->hash($this->input->post('password'));
        $data_user['email']           = $this->input->post('email');
        $data_user['activation_code'] = md5(time());
        $data_user['created_on']      = time();
        $data_user['active']          = '0';
        $this->db->insert('frum_users', $data_user);
        
        $detail = $this->db->get_where('frum_user')->row();
        
        $data_meta['user_id']         = $detail->user_id;
        $data_meta['first_name']      = $this->input->post('first_name');
        $data_meta['last_name']       = $this->input->post('last_name');
        $data_meta['gender']          = $this->input->post('gender');
        $data_meta['No_KTP']          = $this->input->post('No_KTP');
        $data_meta['alamat']          = $this->input->post('alamat');
        $data_meta['Tempat_Lahir']    = $this->input->post('Tempat_Lahir');
        $data_meta['Tanggal_Lahir']   = $this->input->post('Tanggal_Lahir');
        $data_meta['Agama']           = $this->input->post('Agama');
        $data_meta['Status_Perkawinan'] = $this->input->post('Status_Perkawinan');
        $data_meta['Pekerjaan']       = $this->input->post('Pekerjaan');
        $data_meta['Kewarganegaraan'] = $this->input->post('Kewarganegaraan');
        $data_meta['No_Telpon']       = $this->input->post('No_Telpon');
        $this->db->insert('frum_meta', $data_meta);
        
        $data_kend['user_id']         = $detail->user_id;
        $data_kend['Nama_Kesayangan'] = $this->input->post('Nama_Kesayangan');
        $data_kend['Jenis_VW']        = $this->input->post('Jenis_VW');
        $data_kend['Tahun_Produksi']  = $this->input->post('Tahun_Produksi');
        $data_kend['Nomor_Rangka']    = $this->input->post('Nomor_Rangka');
        $data_kend['Nomor_Plat']      = $this->input->post('Nomor_Plat');
        $data_kend['Nomor_Lambung']   = $this->input->post('Nomor_Lambung');
        $data_kend['Nomor_KTA']       = $this->input->post('Nomor_KTA');
        $this->db->insert('data_kendaraan', $data_kend);
    }
    
    function update_user($id){ // Return Boolean
        if($this->input->post('password')){           
            $data_user['password']    = $this->phpass->hash($this->input->post('password'));
        }
        $data_user['group_id']        = $this->input->post('group_id');
        $data_user['ip_address']      = $_SERVER['HTTP_HOST'];
        $data_user['username']        = $this->input->post('username');
        $data_user['email']           = $this->input->post('email');
        
        $this->db->where('user_id', $id);
        return $this->db->update('frum_users', $data);
    }
}